#include "frontend/window.h"
#include "ui_mainwindow.h"

#include<QKeyEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QFont font("Monospace");
    font.setPixelSize(17);

    QFontMetrics metrics(font);
    int cursorHeight = metrics.height();
    int cursorWidth = metrics.width(QChar('Q'));
    int ypos = cursorHeight-metrics.descent()/2;
    int statusBarHeight = statusBar()->height();
    resize(cursorWidth*80, cursorHeight*25+ypos+statusBarHeight);
    setWindowTitle(tr("Terminate Demo"));
    setAutoFillBackground(true);
    QPalette p = palette();
    p.setColor(QPalette::Background,QColor(48,10,36));
    setPalette(p);
    terminate = new Terminate(this);
    setCentralWidget(terminate);
    updateStatusBar();
}


void MainWindow::keyPressEvent(QKeyEvent  *event){
    updateStatusBar();
}

void MainWindow::updateStatusBar(){
    Teletype* tty = terminate->GetTty();
    int row = tty->buffer->curRow + 1;
    int column = tty->buffer->curColumn + 1;
    QString rowStr = QString::number(row);
    QString columnStr = QString::number(column);
    QString status = "Line" ;
    QString s = status.append(rowStr).append(",column").append(columnStr);
    statusBar()->showMessage(s);
}

MainWindow::~MainWindow()
{
    delete ui;
}
